package com.freedom.admin.interfaces.controller;

import com.freedom.admin.application.service.cmd.DeptCmdService;
import com.freedom.admin.application.service.query.DeptQueryService;
import com.freedom.admin.infrastructure.persistence.pojo.form.DeptForm;
import com.freedom.admin.infrastructure.persistence.pojo.query.DeptQuery;
import com.freedom.admin.infrastructure.persistence.pojo.vo.dept.DeptTreeVO;
import com.freedom.admin.infrastructure.persistence.pojo.vo.dept.DeptVO;
import com.freedom.common.core.base.BaseIntsForm;
import com.freedom.common.core.result.CommonResult;
import com.freedom.common.framework.pojo.OptionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "「管理端」部门接口")
@RestController
@RequestMapping("/api/dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final DeptQueryService deptQueryService;

    private final DeptCmdService deptCmdService;

    @ApiOperation(value = "部门列表")
    @GetMapping("/tree")
    public CommonResult<List<DeptTreeVO>> listDeptTree(DeptQuery queryParam) {
        return CommonResult.success(deptQueryService.listDeptTree(queryParam));
    }

    @ApiOperation(value = "部门下拉列表")
    @GetMapping("/select_list")
    public CommonResult<List<OptionVO>> listDeptOption() {
        return CommonResult.success(deptQueryService.listDeptOption());
    }

    @ApiOperation(value = "部门表单数据")
    @GetMapping("/{deptId}/form_data")
    public CommonResult<DeptVO> findDeptDetail(@ApiParam("部门ID") @PathVariable Integer deptId) {
        return CommonResult.success(deptQueryService.findDeptDetail(deptId));
    }

    @ApiOperation(value = "新增部门")
    @PostMapping
    public CommonResult<Boolean> addDept(@RequestBody @Validated DeptForm deptForm) {
        return CommonResult.success(deptCmdService.addDept(deptForm));
    }

    @ApiOperation(value = "修改部门")
    @PutMapping(value = "/{deptId}")
    public CommonResult<Boolean> modifyDept(@ApiParam("部门ID") @PathVariable Integer deptId, @RequestBody @Validated DeptForm deptForm) {
        deptForm.setId(deptId);
        return CommonResult.success(deptCmdService.modifyDept(deptForm));
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/ids")
    public CommonResult<Boolean> removeDept(@RequestBody @Validated BaseIntsForm baseIntsForm) {
        return CommonResult.success(deptCmdService.removeDept(baseIntsForm.getIds()));
    }

}
